package com.linchong.xshop.service.impl;

import com.linchong.xshop.dao.ProductCategoryDao;
import com.linchong.xshop.dao.ProductDao;
import com.linchong.xshop.dto.ProductCategoryExecution;
import com.linchong.xshop.entity.ProductCategory;
import com.linchong.xshop.enums.ProductCategoryStateEnum;
import com.linchong.xshop.exceptions.ProductCategoryOperationException;
import com.linchong.xshop.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @BelongsProject:xshop
 * @BelongsPackage:com.linchong.xshop.service.impl
 * @Author:linchong
 * @CreateTime:2018-08-05 12:50
 * @Description:店铺类别服务类
 */

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {


    @Autowired
    private ProductCategoryDao productCategoryDao;

    @Autowired
    private ProductDao productDao;

    @Override
    public List<ProductCategory> getProductCategoryList(long shopId) {
      return productCategoryDao.queryProductCategoryList(shopId);
    }

    @Override
    @Transactional
    public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList) throws ProductCategoryOperationException {

        if(productCategoryList!=null&&productCategoryList.size()>0){
            try{
                int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
                if(effectedNum<=0){
                    throw new ProductCategoryOperationException("店铺创建失败");
                }else{
                    return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
                }
            }catch (Exception e){
               throw new ProductCategoryOperationException("batchAddProductCategory error"+e.getMessage());
            }
        }else{
            return new ProductCategoryExecution(ProductCategoryStateEnum.EMPTY_LIST);
        }
    }

//    @Override
//    @Transactional
//    public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws ProductCategoryOperationException {
//        //TODO:将此商品类别下的商品的类别Id置为空，操作商品时实现
//        try{
//            int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId,shopId);
//            if(effectedNum <= 0){
//                throw new ProductCategoryOperationException("店铺商品类别删除失败");
//            }else{
//                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
//            }
//        }catch (Exception e){
//            throw new ProductCategoryOperationException("deleteProductCategory error:"+e.getMessage());
//        }
//    }

    @Override
    @Transactional
    public ProductCategoryExecution deleteProductCategory(long productCategoryId,long shopId)throws ProductCategoryOperationException{

        //将商品类别下的商品的类别Id置为空,接触商品和类别的关联
        try{
            int effectedNum = productDao.updateProductCategoryToNull(productCategoryId);
            if(effectedNum < 0){
                throw new ProductCategoryOperationException("商品类别更新失败！");
            }
        }catch (Exception e){
            throw new ProductCategoryOperationException("deleteProductCategory error:"+e.getMessage());
        }

        //删除商品类别
        try{
            int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId,shopId);
            if(effectedNum<=0){
                throw new ProductCategoryOperationException("商品类别删除失败");
            }else{
                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
            }
        }catch (Exception e){
            throw new ProductCategoryOperationException("deleteProductCategory error:"+e.getMessage());
        }
    }
}
